package cn.jy.nvr.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtils {
	
	 private static JedisPool jedisPool = null;
	 static{
		 JedisPoolConfig config = new JedisPoolConfig();
		    config.setMaxIdle(2);//最小连接
			config.setMaxTotal(5);//最大连接
			config.setMaxWaitMillis(2*1000);//超时时间
			config.getTestOnBorrow();//获取连接时测试连接是否可用
			jedisPool=new JedisPool(config, "127.0.0.1");
			System.out.println("获取redis成功");
	 }
	 public static JedisPool getPool() {
	        return jedisPool;
	    }
    
	 public static Jedis getJedis() {
	        return jedisPool.getResource();
	    }
	 
	 public static void returnJedis(Jedis jedis) {
	        if(jedis!=null)
	            jedisPool.returnResource(jedis);
	    }
    
     public static void set(String key,String value ){
    	 if(key==null)return;
    	 Jedis jedis = getJedis();
    	 jedis.lpush(key, value);
    	 returnJedis(jedis);
     }
     public static String get(String key){
    	 Jedis jedis = getJedis();
    	 String value = jedis.rpop(key);
    	 returnJedis(jedis);
    	 return value;
     }
     
}
